Controlling memory conditions in a virtual machine

ABSTRACT

A method of managing host physical memory using a balloon application executing within a guest virtual machine (GVM) running on a host platform is described. The balloon application receives allocation parameters from an entity outside the GVM, the allocation parameters identifying an amount of memory for the balloon application to allocate. The balloon application adjusts the allocated amount of memory according to the allocated amount. Physical memory backing up the allocated memory can then be assigned by virtualization software for use by another virtual machine running on the host platform.

CROSS REFERENCE TO RELATED APPLICATIONS

This application claims the benefit of U.S. Pat. No. 8,095,931, havingthe same title as the present application and [to be] granted on Jan.10, 2012, which in turn claims benefit of U.S. Provisional ApplicationNo. 60/796,069, filed Apr. 27, 2006, both of which are incorporated byreference in their entireties.

BACKGROUND OF THE INVENTION

1. Field of the Invention

The invention relates generally to virtualized computer systems, andspecifically to memory management for a virtual machine.

2. Description of Related Art

The advantages of virtual machine technology have become widelyrecognized. Among these advantages is the ability to run multiplevirtual machines on a single host platform. This makes better use of thecapacity of the hardware, while still ensuring that each user enjoys thefeatures of a “complete” computer. Depending on how it is implemented,virtualization also provides greater security, since the virtualizationcan isolate potentially unstable or unsafe software so that it cannotadversely affect the hardware state or system files required for runningthe physical (as opposed to virtual) hardware.

As is well known in the field of computer science, a virtual machine(VM) is a software abstraction—a “virtualization”—of an actual physicalcomputer system. FIG. 1 shows one possible arrangement of a computersystem 700 that implements virtualization. A virtual machine (VM) 200,which in this system is a “guest,” is installed on a “host platform,” orsimply “host,” which will include a system hardware 100, that is, ahardware platform, and one or more layers or co-resident componentscomprising system-level software, such as an operating system (OS) orsimilar kernel, a virtual machine monitor or hypervisor (see below), orsome combination of these.

As software, the code defining the VM will ultimately execute on theactual system hardware 100. As in almost all computers, this hardwarewill include one or more CPUs 110, some form of memory 130 (volatile ornon-volatile), one or more storage devices such as one or more disks140, and one or more devices 170, which may be integral or separate andremovable.

In many existing virtualized systems, the hardware processor(s) 110 arethe same as in a non-virtualized computer with the same platform, forexample, the Intel x86 platform. Because of the advantages ofvirtualization, however, some hardware vendors are producing hardwareprocessors that include specific hardware support for virtualization.

Each VM 200 will typically mimic the general structure of a physicalcomputer and as such will usually have both virtual system hardware 201and guest system software 202. The virtual system hardware typicallyincludes at least one virtual CPU 210, virtual memory 230, at least onevirtual disk 240, and one or more virtual devices 270. Note that astorage disk—virtual 240 or physical 140—is also a “device,” but isusually considered separately because of the important role it plays.All of the virtual hardware components of the VM may be implemented insoftware to emulate corresponding physical components. The guest systemsoftware includes a guest operating system (OS) 220 and drivers 224 asneeded, for example, for the various virtual devices 270.

To permit computer systems to scale to larger numbers of concurrentthreads, systems with multiple CPUs—physical or logical, or acombination—have been developed. One example is a symmetricmulti-processor (SMP) system, which is available as an extension of thePC platform and from other vendors. Essentially, an SMP system is ahardware platform that connects multiple processors to a shared mainmemory and shared I/O devices. Yet another configuration is found in aso-called “multi-core” architecture, in which more than one physical CPUis fabricated on a single chip, with its own set of functional units(such as a floating-point unit and an arithmetic/logic unit ALU), andcan execute threads independently; multi-core processors typically shareonly very limited resources, such as at least some cache. Still anothertechnique that provides for simultaneous execution of multiple threadsis referred to as “simultaneous multi-threading,” in which more than onelogical CPU (hardware thread) operates simultaneously on a single chip,but in which the logical CPUs flexibly share not only one or morecaches, but also some functional unit(s) and sometimes also thetranslation lookaside buffer (TLB).

Similarly, a single VM may (but need not) be configured with more thanone virtualized physical and/or logical processor. By way of example,FIG. 1 illustrates multiple virtual processors 210, 211, . . . , 21 m(VCPU0, VCPU1, . . . , VCPUm) within the VM 200. Each virtualizedprocessor in a VM may also be multi-core, or multi-threaded, or both,depending on the virtualization. This invention may be used to advantageregardless of the number of processors the VMs are configured to have.

If the VM 200 is properly designed, applications 260 running on the VMwill function essentially as they would if run on a “real” computer,even though the applications are running at least partially indirectly,that is via the guest OS 220 and virtual processor(s). Executable fileswill be accessed by the guest OS from the virtual disk 240 or virtualmemory 230, which will be portions of the actual physical disk 140 ormemory 130 allocated to that VM. Once an application is installed withinthe VM, the guest OS retrieves files from the virtual disk just as ifthe files had been pre-stored as the result of a conventionalinstallation of the application. The design and operation of virtualmachines in general are known in the field of computer science.

Some interface is generally required between the guest software within aVM and the various hardware components and devices in the underlyinghardware platform. This interface—which may be referred to generally as“virtualization software” or “virtualization layer”—may include one ormore software components and/or layers, possibly including one or moreof the software components known in the field of virtual machinetechnology as “virtual machine monitors” (VMMs), “hypervisors,” orvirtualization “kernels.” Because virtualization terminology has evolvedover time and has not yet become fully standardized, these terms do notalways provide clear distinctions between the software layers andcomponents to which they refer. For example, “hypervisor” is often usedto describe both a VMM and a kernel together, either as separate butcooperating components or with one or more VMMs incorporated wholly orpartially into the kernel itself; however, “hypervisor” is sometimesused instead to mean some variant of a VMM alone, which interfaces withsome other software layer(s) or component(s) to support thevirtualization. Moreover, in some systems, some virtualization code isincluded in at least one “superior” VM to facilitate the operations ofother VMs. Furthermore, specific software support for VMs is sometimesincluded in the host OS itself.

Unless otherwise indicated, the invention described below may be used invirtualized computer systems having any type or configuration ofvirtualization software. Moreover, the invention is described andillustrated below primarily as including one or more virtual machinemonitors that appear as separate entities from other components of thevirtualization software. This is only for the sake of simplicity andclarity and by way of illustration—as mentioned above, the distinctionsare not always so clear-cut. Again, unless otherwise indicated orapparent from the description, it is to be assumed that the inventioncan be implemented with components residing anywhere within the overallstructure of the virtualization software.

By way of illustration and example only, the figures show each VMrunning on a corresponding virtual machine monitor. The description'sreference to VMMs is also merely by way of common example. A VMM isusually a software component that virtualizes at least one hardwareresource of some physical platform, so as to export a hardware interfaceto the VM corresponding to the hardware the VM “thinks” it is runningon. As FIG. 1 illustrates, a virtualized computer system may (andusually will) have more than one VM, each of which may be running on itsown VMM.

The various virtualized hardware components in the VM, such as thevirtual CPU(s) 210, etc., the virtual memory 230, the virtual disk 240,and the virtual device(s) 270, are shown as being part of the VM 200 forthe sake of conceptual simplicity. In actuality, these “components” areoften implemented as software emulations included in some part of thevirtualization software, such as the VMM. One advantage of such anarrangement is that the virtualization software may (but need not) beset up to expose “generic” devices, which facilitate, for example,migration of VM from one hardware platform to another.

Different systems may implement virtualization to differentdegrees—“virtualization” generally relates to a spectrum of definitionsrather than to a bright line, and often reflects a design choice inrespect to a trade-off between speed and efficiency on the one hand andisolation and universality on the other hand. For example, “fullvirtualization” is sometimes used to denote a system in which nosoftware components of any form are included in the guest other thanthose that would be found in a non-virtualized computer; thus, the guestOS could be an off-the-shelf, commercially available OS with nocomponents included specifically to support use in a virtualizedenvironment.

In contrast, another concept, which has yet to achieve a universallyaccepted definition, is that of “para-virtualization.” As the nameimplies, a “para-virtualized” system is not “fully” virtualized, butrather the guest is configured in some way to provide certain featuresthat facilitate virtualization. For example, the guest in somepara-virtualized systems is designed to avoid hard-to-virtualizeoperations and configurations, such as by avoiding certain privilegedinstructions, certain memory address ranges, etc. As another example,many para-virtualized systems include an interface within the guest thatenables explicit calls to other components of the virtualizationsoftware. For some, para-virtualization implies that the guest OS (inparticular, its kernel) is specifically designed to support such aninterface. According to this view, having, for example, an off-the-shelfversion of Microsoft Windows XP as the guest OS would not be consistentwith the notion of para-virtualization. Others definepara-virtualization more broadly to include any guest OS with any codethat is specifically intended to provide information directly to theother virtualization software. According to this view, loading a modulesuch as a driver designed to communicate with other virtualizationcomponents renders the system para-virtualized, even if the guest OS assuch is an off-the-shelf, commercially available OS not specificallydesigned to support a virtualized computer system.

Unless otherwise indicated or apparent, this invention is not restrictedto use in systems with any particular “degree” of virtualization and isnot to be limited to any particular notion of full or partial (“para-”)virtualization.

In addition to the distinction between full and partial (para-)virtualization, two arrangements of intermediate system-level softwarelayer(s) are in general use—a “hosted” configuration (illustrated inFIG. 2) and a non-hosted configuration (illustrated in FIG. 1). In ahosted virtualized computer system, an existing, general-purposeoperating system forms a “host” OS that is used to perform certaininput/output (I/O) operations, alongside and sometimes at the requestand direction of the VMM 300. The host OS 420, which usually includesdrivers 424 and supports applications 460 of its own, and the VMM areboth able to directly access at least some of the same hardwareresources, with conflicts being avoided by a context-switchingmechanism. The Workstation product of VMware, Inc., of Palo Alto,Calif., is an example of a hosted, virtualized computer system.

In addition to device emulators 370, FIG. 2 also illustrates some of theother components that are also often included in the VMM of a hostedvirtualization system; many of these components are found in the VMM ofa non-hosted system as well. For example, exception handlers 330 may beincluded to help context-switching, and a direct execution engine 310and a binary translator 320 with associated translation cache 325 may beincluded to provide execution speed while still preventing the VM fromdirectly executing certain privileged instructions.

In many cases, it may be beneficial to deploy VMMs on top of a softwarelayer—a kernel 600—constructed specifically to provide efficient supportfor the VMs. This configuration is frequently referred to as being“non-hosted.” Compared with a system in which VMMs run directly on thehardware platform (such as shown in FIG. 2), use of a kernel offersgreater modularity and facilitates provision of services (for example,resource management) that extend across multiple virtual machines.Compared with a hosted deployment, a kernel may offer greaterperformance because it can be co-developed with the VMM and be optimizedfor the characteristics of a workload consisting primarily of VMs/VMMs.The kernel 600 also handles any other applications running on it thatcan be separately scheduled, as well as any “console” operating system420 that, in some systems, is included to boot the system as a whole andfor enabling certain user interactions with the kernel. The console OSin FIG. 1 may be of the same type as the host OS in FIG. 2, which is whythey are identically numbered—the main difference is the role they play(or are allowed to play, if any) once the virtualized computer system isloaded and running.

This invention may be used to advantage in both a hosted and anon-hosted virtualized computer system, in which the included virtualmachine(s) may be fully or para-virtualized, and in which the virtualmachine(s) have any number of virtualized processors, which may be ofany type (including multi-core, multi-threaded, or some combination).The invention may also be implemented directly in a computer's primaryOS, both where the OS is designed to support virtual machines and whereit is not. Moreover, the invention may even be implemented wholly orpartially in hardware, for example in processor architectures intendedto provide hardware support for virtual machines.

To facilitate effective memory management, many operating systems in usetoday introduce a layer of abstraction between the memory addresses usedby the applications and the memory addresses describing physical memory.When an application requests memory, the operating system will allocatememory in a first address space, typically called a virtual memoryaddress space. This first memory address space maps to a second memoryaddress space, typically the physical memory of the computer. A pagetable organizes the relationships between the two address spaces andmaps memory addresses (for example, given as page numbers) of the firstmemory address space to memory addresses of the second memory addressspace. It is common for multiple virtual memory address spaces, as wellas multiple page tables, to be implemented in modern operating systems.For example, each application may have its own virtual memory addressspace. In many systems, each application can treat its virtual memoryaddress space as if it had exclusive use of that memory. The operatingsystem organizes these virtual memory addresses spaces and keeps trackof the corresponding physical memory address using entries in a pagetable.

One of the advantages of using virtual memory address spaces is that theamount of virtual memory used by the applications may exceed the amountof physical memory available on the computer. When such a situationoccurs, the operating system will use a secondary storage medium, suchas a hard disk, to store some of the data contained in virtual memory.When data from some virtual memory pages is actually stored on thesecondary storage medium, the page table will map some virtual memoryaddresses to physical memory addresses, while mapping other virtualmemory addresses to locations on the secondary storage medium.

If an application attempts to access a virtual memory address not mappedto physical memory, the operating system will detect a page fault. Inresponse to a page fault, the operating system will retrieve therequested data from the appropriate storage device, store it in physicalmemory, and update the page table with the address of the location inphysical memory. Retrieving a page and storing it in physical memory iscommonly described as “paging-in” the requested page. Frequently, inorder to page-in some data, the operating system must first make room inthe physical memory. One method for making room in the physical memoryis by “paging-out” a page presently stored in the physical memory.Paging-out refers to the process of copying a page from the physicalmemory to another storage device and updating the page tableaccordingly. Subsequent access to that virtual memory address will thenresult in another page fault and the paging-in process will repeat.Ideally, the operating system will page-out pages that are inactive sothat they will not have to be paged back in for some reasonable amountof time. Various methods for determining which pages are inactive andare good candidates to be paged-out are well known in the art.

When a guest operating system 220 is run on a virtual machine 200, theguest operating system 220 treats the virtual memory 230 as if it werethe physical memory of a computer system. Thus the guest operatingsystem 220 will create virtual memory address spaces (not shown) and mapthem into the virtual memory 230.

The virtualization layer introduces an additional layer of memorymanagement abstraction. The kernel 600 typically emulates the virtualmemory 230 by mapping the virtual memory 230 to the physical memory 130.In many ways, the mapping of the virtual memory 230 to the physicalmemory 130 is analogous of the mapping of virtual memory addresses tophysical memory addresses performed by an operating system. Guestoperating systems 220 running on various virtual machines 200 areallowed to treat their virtual memory 230 as if they had exclusivecontrol over that memory, when in fact those virtual memory addressspaces are mapped to physical memory 130. Furthermore, as in virtualmemory managed by an operating system, the total amount of virtualmemory 230 used by the various virtual machines 200 may exceed the totalamount of physical memory 130. The virtual machine monitor 300 organizesthese virtual memory addresses and keeps track of the correspondingphysical memory addresses in the memory 130.

Thus, when a guest operating system 220 implementing virtual memory isrun on a virtual machine, typically three levels of memory addressspaces are used. The guest operating system 220 organizes some virtualmemory address spaces. For the purposes of this application, theseaddresses spaces are referred to as “guest virtual memory,” which areaddressed using a “guest virtual page number” (GVPN). The guestoperating system 220 maintains a page table that maps this guest virtualmemory to the virtual memory 230. Typically, the guest operating system220 treats the virtual memory 230 as if it were physical memory. For thepurposes of this application, the address space of the virtual memory230 is referred to as the “guest physical memory,” which is addressedusing a “guest physical page number” (GPPN). The virtual machine monitor300 maintains a data structure (such as a page table) that maps thisguest physical memory to the physical memory 130. The physical memory130 is addressed using a “physical page number” (PPN), which issometimes also referred to as a “machine page number” (MPN).

One approach for allowing the total amount of guest physical memory usedby the various virtual machines 200 to exceed the total amount ofphysical memory is for the kernel 600 to page-out some of the inactiveguest physical memory. For example, the kernel 600 can copy pages fromthe physical memory 130 to the disk 140 and adjust the page table entryfor the corresponding guest physical memory accordingly. However, suchan approach has several problems. First of all, determining which guestphysical memory pages are less active (and therefore suitable forpage-out) is a challenging task, and it is difficult to find inactivepages with a high degree of accuracy. Paging-out a page that is activelybeing used will result in a page fault and require the selection ofanother page for page-out in the near future. The subsequent paging andrepaging cycle can seriously affect the performance of the system, andin worst cases, can bring the virtual machine to an effective halt, acondition is commonly referred to as “thrashing”.

Furthermore, even if an inactive page of the guest physical memory isdetermined with a high degree of accuracy, paging-out this pageintroduces a problem known as “double-paging.” If the guest physicalmemory is constrained, the guest operating system 220 will be searchingfor inactive guest virtual memory pages as candidates for page-out. Whenit finds an inactive guest virtual memory page, it will attempt to readfrom the corresponding guest physical memory page to perform thepage-out. However, since the guest physical memory page is alsoinactive, it may have already been paged-out by the kernel 600. If theguest physical memory page has already been paged-out by the kernel 600,the attempt to page-out the guest virtual memory will result in a guestphysical memory page fault, and the inactive guest physical page will bepaged back in. Thus interference between the memory management of theguest operating system 220 and the kernel 600 can significantly reducethe effectiveness of paging-out guest physical memory pages.

Another approach for managing allocations of physical memory to variousvirtual machines is to dynamically change the size of the guest physicalmemory. However, most guest operating systems 220 do not provide amechanism by which the amount of guest physical memory can be increasedor decreased during execution of the operating system. Therefore,current techniques for managing allocations of physical memory tovarious virtual machines are inadequate.

SUMMARY OF THE INVENTION

The invention is a system and method for controlling memory conditionsin a virtual machine. In one embodiment, a resource reservationapplication running as a guest application on the virtual machinerequests a location in guest virtual memory from the guest operatingsystem. The guest operating system allocates a location in the guestvirtual memory to the application and maps the guest virtual memorylocation to a location in the guest physical memory. This location inguest physical memory is mapped to a physical memory location, which canbe reclaimed. The location in physical memory is reclaimed by thekernel, and can be assigned to other virtual machines or maintained in apool of available physical memory locations.

Because the resource reservation application reserves a location inguest virtual memory, the location allocated to the application canpotentially be paged out by the guest operating system. In order toeffectively implement the reclaiming of memory, the invention furtherincludes systems and methods for indicating to the guest operatingsystem that the location in guest virtual memory is active to reducepage-out, and for determining if the guest operating system haspaged-out the location in guest virtual memory. Furthermore, thedetermination of guest virtual memory page-out by the guest operatingsystem is useful for determining memory conditions inside the guestoperating system, and can be used for adjusting physical memoryallocations to the various virtual machines.

BRIEF DESCRIPTION OF THE DRAWINGS

FIG. 1 (prior art) shows one example of a virtualized computer system inwhich the present invention can operate.

FIG. 2 (prior art) shows another example of a virtualized computersystem in which the present invention can operate.

FIG. 3 illustrates a resource request application executing in a virtualmachine, according to one embodiment of the present invention.

FIG. 4 illustrates a three address space memory mapping, according toone embodiment of the present invention.

FIG. 5A illustrates use of a physical memory address space by two ormore virtual machines, according to one embodiment of the presentinvention.

FIG. 5B illustrates executing a resource request application forreclaiming physical memory allocated to a virtual machine, according toone embodiment of the present invention.

FIG. 5C illustrates another embodiment for the reclaiming of physicalmemory allocated to a virtual machine.

FIG. 5D illustrates reallocating physical memory to a virtual machine inresponse to the paging-out of guest virtual memory allocated to theresource request application.

FIG. 6 illustrates a method used by a resource request application,according to one embodiment of the present invention.

FIG. 7 illustrates a method for supporting a resource reservationapplication, according to one embodiment of the present invention.

FIG. 8 illustrates a method managing memory resources, according to oneembodiment of the present invention.

FIG. 9 illustrates a method for reclaiming physical memory, according toone embodiment of the present invention.

FIG. 10 illustrates a method for supporting a resource reservationapplication, according to another embodiment of the present invention.

FIG. 11 illustrates a method managing memory resources, according toanother embodiment of the present invention.

FIG. 12 illustrates a method for reclaiming physical memory, accordingto another embodiment of the present invention.

FIG. 13 illustrates a method for determining memory conditions inside avirtual machine.

FIG. 14 illustrates a method for controlling memory conditions inside avirtual machine.

DETAILED DESCRIPTION OF THE PREFERRED EMBODIMENTS

A preferred embodiment of the present invention is now described withreference to the figures where like reference numbers indicate identicalor functionally similar elements. Also in the figures, the left mostdigits of each reference number correspond to the figure in which thereference number is first used.

The invention is a system and method for reclaiming memory assigned to avirtual machine. A resource reservation application (called a “balloonapplication”) running as a guest application in the virtual machinerequests a location in the guest virtual memory from the guest operatingsystem. According to one embodiment of the present invention, theballoon application also periodically accesses the allocated guestvirtual memory location. Depending on memory conditions in the virtualmachine, such accesses either keep the guest virtual memory locationmapped to a location in guest physical memory (i.e. they preempt apage-out by the guest operating system) or cause a page-fault (i.e. theyforce the guest operating system to page-in the location in guestphysical memory.) By reserving guest virtual memory, the balloonapplication assists with the reclamation of physical memory.

The balloon application is responsive to commands sent from outside thevirtual machine (for example, from the virtual machine monitor). Thevirtualization layer may use these commands to influence the balloonapplication's usage of guest virtual memory. For example, the balloonapplication can receive commands indicating the size of the allocationto request from the guest operating system, and the frequency with whichto access that memory. Reserving guest virtual memory using a balloonapplication provides increased control freedom compared to reservingguest physical memory directly. Such control freedom allows for moresubtle manipulation of memory conditions in the virtual machine.Periodically accessing guest virtual memory maintains or createsallocations of guest physical memory, which assists the kernel 600 withthe reclamation of physical memory for the virtual machine.

To facilitate communication between the balloon application and thevirtualization layer, a device driver (sometimes referred to as a“balloon application driver”) can be included in the guest operatingsystem 220. The balloon application driver is useful for sending andreceiving messages to and from the balloon application, as well as fordetecting page-out of the allocated guest virtual memory by the guestoperating system. Furthermore, as an added optimization, the balloonapplication driver can implement techniques causing the allocatedlocation to appear to the guest operating system as if it had beenaccessed, without the step of actually accessing that location.

As described above, FIGS. 1 and 2 depict hosted and non-hostedimplementations of virtualized computer systems. The invention isapplicable at least to both such implementations of virtualized computersystems, as well as para-virtualized computer systems. For the purposesof illustration, an implementation of the invention is discussedinvolving the mapping of guest virtual memory to guest physical memory,which is in turn mapped to physical memory. This example has been chosenfor the purposes of illustration and is not limiting. The invention maybe beneficially implemented in any virtualized computer system having atleast two abstraction layers for memory addressing. For example, in ahosted implementation, it is possible that the address space consideredby the kernel 600 to be physical memory could in fact be virtual memoryimplemented by a host operating system, or potentially anothervirtualization layer running between the kernel 600 and the systemhardware 100. In another embodiment, the guest physical memory can bemanaged by an alternative type of virtualization layer, such as aruntime environment or an emulator. One of skill in the art willrecognize that the systems and methods described herein may be used withother potential memory address space configurations without departingfrom the scope of the present invention.

FIG. 3 illustrates a resource request application executing on a virtualmachine, according to one embodiment of the present invention. A balloonapplication 261 executes as a guest application 260 on the virtualmachine 200. According to one embodiment of the present invention, theballoon application 261 executes as a user-level application. Theballoon application 261 is not privileged, and therefore cannottypically reserve arbitrary amounts of guest physical memory. Accordingto another embodiment of the present invention, the balloon application261 executes with special permissions, for example, by running in kernelmode, that allow it to perform certain actions not ordinarily permittedby user-level applications. A method used by the balloon applicationwill be described herein with reference to FIG. 6.

According to one embodiment of the present invention, the guestoperating system 220 also includes an optional balloon applicationdriver 225. The balloon application driver 225 executes as a devicedriver within the guest operating system 220 and is capable ofperforming certain privileged actions. For example, the balloonapplication driver 225 may be able to reserve guest physical memory, toperform input/output instructions, to write to protected areas of guestphysical memory, and to modify page tables. The balloon applicationdriver 225 may also be capable of performing other actions typicallyperformed by operating system device drivers.

According to one embodiment of the present invention, the balloonapplication 261 sends messages to the balloon application driver 225.The balloon application 261 may send messages to the balloon applicationdriver 225 using a variety of methods. For example, the balloonapplication 261 can open a handle to a device managed by the balloonapplication driver 225. In a typical guest operating system 220, systemcalls to this device will be handled by the balloon application driver225. As another example, the balloon application 261 can make anexplicit driver call to the balloon application driver 225. Othermethods will be apparent to one of skill in the art without departingfrom the scope of the present invention.

According to one embodiment of the present invention, the balloonapplication driver 225 sends messages to the balloon application 261.The balloon application driver 225 may send messages to the balloonapplication 261 using a variety of methods. For example, the balloonapplication driver 225 typically has direct access to guest physicalmemory. The balloon application driver 225 can communicate with theballoon application 261 by writing messages into the guest physicalmemory to which the guest virtual memory allocated to the balloonapplication 261 is mapped. As another example, the balloon application261 can run in a sleep state, and the balloon application driver 225 canuse synchronization primitives to “wake” the balloon application 261.

Outside of the virtual machine 200 is a resource scheduler 601. For thepurposes of illustration, the resource scheduler 601 is shown as beingcontained in the kernel 600. However, the systems and methods ascribedherein to the resource scheduler 601 may be embodied in other componentsin the virtualization layer. For example, the resource scheduler 601 canbe embodied entirely in the virtual machine monitor 300. As anotherexample, the resource scheduler 601 can be embodied partially in thevirtual machine monitor 300 and partially in the kernel 600. As yetanother example, the resource scheduler 601 can be embodied in the hostoperating system of a paravirtualized machine. Other implementations forthe resource scheduler 601 will be apparent to one of skill in the artwithout departing from the scope of the present invention.

The resource scheduler 601 manages the assignment of resources to thevarious virtual machines 200. One such resource is physical memory.According to one embodiment of the present invention, the resourcescheduler 601 monitors the virtual machines' 200 usage of theirrespective guest physical memories to determine how much physical memoryshould be allocated to each virtual machine 200. The resource scheduler601 may also take into account the relative priorities of the variousvirtual machines 200. The resource scheduler 601 may use a variety ofmethods to characterize the usage of a virtual machine's guest physicalmemory. The resource scheduler 601 may allocate more physical memory tovirtual machines actively using a greater percentage of their guestvirtual memory than virtual machines using a lower percentage of theirguest virtual memory, for example using a method such as one of thosedescribed in “Proportional Share Resource Allocation with Reduction ofUnproductive Resource Consumption,” U.S. patent application Ser. No.09/952,796, filed Sep. 12, 2001, by Carl Waldspurger, which is herebyincorporated by reference in its entirety.

If the resource scheduler 601 determines that the amount of physicalmemory allocated to a virtual machine should be adjusted, the resourcescheduler 601 may reclaim the physical memory using a variety ofmethods. For example, the resource scheduler 601 may page-out some ofthe guest physical memory to another storage device, effectivelyreducing the amount of physical memory allocated to the virtual machine.As another example, the resource scheduler 601 may reclaim some of thephysical memory from the inside of the virtual machine by using theballoon application 261 to reduce the amount of guest physical memoryused by the other applications 260. As another example the resourcescheduler 601 may reclaim some of the physical memory from the inside ofthe virtual machine by using a device driver to reduce the amount ofguest physical memory used by the applications 260. As yet anotherexample, the resource scheduler 601 may reclaim the physical memoryusing any combination of methods described herein or known in the art. Amethod used by the resource scheduler 601 is described herein withreference to FIGS. 8, 11, and 13.

According to one embodiment of the present invention, the resourcescheduler 601 reclaims guest physical memory by sending allocationparameters to the balloon application 261. According to one embodimentof the present invention, the resource scheduler 601 communicates withthe balloon application 261 through the balloon application driver 225.The resource scheduler 601 may communicate with the balloon applicationdriver 225 using a variety of methods. For example, the resourcescheduler 601 and balloon application driver 225 may communicate throughshared memory. According to one embodiment of the present invention,communication between the resource scheduler 601 and the balloonapplication driver 225 is facilitated by the virtual machine monitor300. For example, the virtual machine monitor 300 may trap oninput/output calls, memory mapped input/output instructions, or specialor undefined instructions from the balloon application driver 225 andforward these messages to the resource scheduler 601. As anotherexample, the balloon application driver 225 can execute a hypercall (forexample, in a paravirtualized system) requesting assistance from thevirtual machine monitor 300. As another example, the balloon applicationdriver 225 can execute a specialized call requesting hardwareassistance. As yet another example, the virtual system hardware 201 caninclude a virtual device through which the balloon application driver225 and the virtual machine monitor 300 can communicate.

By operating in conjunction with the guest operating system 220 andhaving kernel permissions, the balloon application driver 225 mayfacilitate communication between the balloon application 261 and theresource scheduler 601. Furthermore, the balloon application driver 225is also useful for determining the occurrence of guest virtual memorypaging by the guest operating system 220.

For the purposes of illustration, the resource scheduler 601 is showncommunicating with the balloon application driver 225 through theballoon application driver 225. According to one embodiment of thepresent invention, the balloon application 261 communicates with theresource scheduler 601 without the use of a balloon application driver.For example, the balloon application 261 can invoke the virtual machinemonitor directly by executing special instructions, such as aninstruction that is not defined in the instruction set architecture ofthe virtual CPU 210. The virtual machine monitor can trap these specialinstructions and pass messages to the resource scheduler 601. As anotherexample, the resource scheduler 601 can send messages to the balloonapplication 261 through the virtual machine monitor by writing to sharedmemory, or by sending a special virtual interrupt. Other methods forcommunicating between the resource scheduler 601 and the balloonapplication 261 will be apparent to one of skill in the art withoutdeparting from the scope of the present invention.

FIG. 4 illustrates a three-level memory address space, according to oneembodiment of the present invention. A memory addressing configurationhaving three levels of address spaces has been selected for the purposesof illustration as an example of configuration in which the inventionmay be usefully implemented. Moreover, the address spaces describedherein, specifically, guest virtual memory, guest physical memory, andphysical memory, are given for the purposes of illustration and are notlimiting. Other configurations and examples of address spaces will beapparent to one of skill in the art without departing from the scope ofthe present invention.

When multiple address spaces are implemented, different memory addresses(to use a specific example of a “memory location”) sometimes refer tothe same place in the electronic storage. For example, the same bits inelectronic storage may be described by a guest virtual memory address, aguest physical memory address, and a physical memory address. As anotherexample, the same bits in electronic storage may be described by twodistinct guest physical memory addresses. The number of addressesreferencing a set of bits of electronic storage should not be confusedwith their innate storage capacity, which is generally fixed.

Typically, the relationships between addresses in one address space andthe addresses in another address space are maintained in a page table. Apage table is composed of page table entries. Each page table entry mapsan address in the first address space to an address in the secondaddress space. These addresses are described using page numbers. Thus, apage table entry might include a page number of a first address spaceand a corresponding page number of a second address space.

In the example illustrated in FIG. 4, memory addresses in the guestvirtual memory address space are described by Guest Virtual Page Numbers(GVPNs), and memory addresses in the guest physical memory address spaceare described by Guest Physical Page Numbers (GPPNs). The guestoperating system 220A maintains a page table mapping the GVPNs to GPPNs.If a guest virtual memory page has been paged-out, instead of mapping toa GPPN, its page table entry may map to a location on the virtual disk240A (or may include some other indication that the page is not storedin guest physical memory). For example, a GVPN may be “unmapped,” andnot map to either a valid GPPN or location on the virtual disk 240.

Similarly, memory addresses in the physical memory address space aredescribed by Physical Page Numbers (PPNs). The virtual machine monitor300 (or another component of the virtualization layer) maintains a datastructure (such as a page table) mapping the GPPNs to PPNs. If a guestphysical memory page has been paged-out, instead of mapping to a PPN,its page table entry may map to a location on the disk 140 (or mayinclude some other indication that the page is stored on disk). Asanother case, a GPPN may be “unmapped,” and not map to either a validPPN or location on the disk 140. Unmapped GPPNs are useful forreclaiming physical pages from a virtual machine, as described hereinwith reference to FIG. 5 b.

In the example illustrated, a GVPN 236A is mapped to a GPPN 231A. Themapping of the GVPN 236A is maintained by the guest operating system220A. The GPPN 231A is mapped to a PPN 131A. The mapping of the GPPN231A is maintained by the virtual machine monitor 300.

The disk 140 has been shown as an example of a storage medium forstoring paged-out data. This example has been chosen for the purposes ofillustration and is not limiting. Other examples of media to whichpaged-out data can be stored include secondary caches, flash memory,network-attached storage, and so on. Other examples will be apparent toone of skill in the art without departing from the scope of the presentinvention. Similarly, the virtual disk 240 has been shown as an exampleof a virtual storage medium for storing paged-out data. This example hasbeen chosen for the purposes of illustration and is not limiting. Otherexamples of real or virtual media to which paged-out data can be storedinclude secondary caches, flash memory, network destinations, and so on.Other examples will be apparent to one of skill in the art withoutdeparting from the scope of the present invention.

At each level of memory management abstraction, memory addresses may betreated as if they were tangible memory locations. For example, supposea guest application writes data to a guest virtual memory locationdescribed by the GVPN 236A. To the guest application, this memorylocation is the destination in which the data will be written. However,at another layer of abstraction, the guest virtual memory locationdescribed by the GVPN 236A maps to a guest physical memory locationdescribed by the GPPN 231A. To the guest operating system 220A, thismemory location is the destination in which the data will be written.But again, at yet another layer of abstraction, the guest physicalmemory location described by the GPPN 231A maps to a physical memorylocation described by the PPN 131A. The same write operation may havethe appearance of writing to three memory locations—a guest virtuallocation, a guest physical location, and a physical location. However,in the example illustrated, the guest virtual location maps to the guestphysical location, which maps to the physical location, so attempting towrite to any of the three locations in fact results in the writing ofdata to the same physical location. A subsequent read from either theguest virtual location, the guest physical location, or the physicallocation (provided the various mappings are still in place) will returnthe data read from the physical location.

FIG. 5A illustrates use of a physical memory address space by two ormore virtual machines, according to one embodiment of the presentinvention. In the example illustrated, virtual machine 200A includesguest virtual memory address space 235A and guest virtual memory addressspace 235B. While two guest virtual memory address spaces have beenshown for the purposes of illustration, other numbers of guest virtualmemory address spaces are commonly implemented in the virtual machines200. The guest virtual memory address spaces map to a guest physicaladdress space 230A. For example, the guest virtual memory address space235A includes a guest virtual page number (GVPN) 236A, which maps to aguest physical page number (GPPN) 231A included in the guest physicalmemory address space 230A. Similarly, the guest virtual address space235B includes a GVPN 236B which maps to a GPPN 232A in the guestphysical address space 230A. As illustrated in the figure, multipleguest virtual memory address spaces may map to a single guest physicalmemory address space. Typically, this mapping is maintained by the guestoperating system 220A.

Similarly, virtual machine 200B includes a guest virtual memory addressspace 235C. While only a single guest virtual memory address space isshown, the virtual machine 200B may contain any number of guest virtualmemory address spaces. The guest virtual memory address space 235C mapsto a guest physical memory address space 230B. For example, the guestvirtual memory address space 235C includes a GVPN 236C which maps to aGPPN 233B in the guest physical memory address space 230B.

The guest physical memory address spaces 230 of the various virtualmachines 200 map to the physical memory address space 130. For example,the guest physical memory address space 230A includes a GPPN 231A thatmaps to a PPN 131A. The guest physical memory address space 230A alsoincludes a GPPN 232A that maps to a PPN 132A. Similarly, the guestphysical memory address space 230B includes a GPPN 233B that maps to aPPN 133B. Typically, this mapping is maintained by the virtualizationlayer.

A guest application may store to and read from physical memory by usingthe guest virtual address. The guest virtual address includes a GVPN,for example the GVPN 236A. The application attempts to write to the pagegiven by the GVPN 236A. A first page table, maintained by the guestoperating system 220A, is used to determine the GPPN to which the GVPNis mapped, in this example the GPPN 231A. A second page table,maintained, for example, by the virtual machine monitor 300, is used todetermine the PPN to which the GPPN is mapped, in this example the PPN131A. The data to be stored by the guest application is then stored onthe physical memory page given by PPN 131A.

As an added optimization, another page table may be maintained mappingthe GVPNs directly to PPNs. Such an optimization may improve theefficiency of memory reads and writes by guest applications, as guestvirtual addresses may be more directly translated to physical addresses.

In the example illustrated, the guest physical memory address spaces 230include a total of six guest physical pages. However, the physicalmemory address space 130 includes a total of five physical pages, threeof which are allocated to virtual machine 200A and two of which areallocated to virtual machine 200B. In the example illustrated, the totalamount of memory in the guest physical memory address spaces is greaterthan the total amount of memory in the physical address space.

While the virtual machines have been illustrated as each having a singleguest physical memory address space, a real or virtual machine could beimplemented having a plurality of guest physical memory address spaces.Similarly, the computer system has been illustrated as having a singlephysical memory address space 130. A computer system could beimplemented having a plurality of memory address spaces 130. Othermemory addressing schemes will be apparent to one of skill in the artwithout departing from the scope of the present invention.

FIG. 5B illustrates executing a resource request application forreclaiming physical memory allocated to a virtual machine, according toone embodiment of the present invention. FIG. 5A illustrated an examplein which three physical pages were allocated to virtual machine 200A.FIG. 5B illustrates an example of how two of these physical pages can bereallocated by use of a balloon application.

A balloon application executes on top of the guest operating system 220Aof virtual machine 200A. The balloon application requests some guestvirtual memory, and the guest operating system 220A allocates at leastone guest virtual page. In the example illustrated, the guest operatingsystem 220A allocates two guest virtual pages in the guest virtualmemory address space 235B to the balloon application. The balloonapplication accesses these memory locations (or otherwise causes them tobe paged-in) and the guest operating system 220A maps the guest virtualpages to guest physical pages. For example, GVPN 236B is mapped to aGPPN 239 describing a location in the guest physical memory addressspace.

In order to accommodate the allocation (or paging-in of the allocation)of the guest virtual pages of the balloon application, the guestoperating system 220A paged-out some of the guest virtual pages of theguest virtual memory address space 235A. For example, GVPN 236A now mapsto a location in the virtual disk 240A. The GPPN to which GVPN 236Apreviously mapped is now available for a different GVPN. In the exampleillustrated, that GPPN is now mapped to by the GVPN 236B. In the exampleillustrated, the balloon application's guest virtual page allocationresulted in the paging-out of guest virtual pages allocated to otherapplications. In other cases, the balloon application's guest virtualpage allocation can result in reductions to other kernel managed memorypages, such as a file buffer cache.

As one of the GVPNs allocated to the balloon application is mapped tothe GPPN 239, the GPPN 239 may be safely paged-out by the resourcescheduler 601. According to one embodiment of the present invention, thedata in the PPN previously mapped to by the GPPN 239 may be copied todisk and the page table updated so that the GPPN 239 map to the locationon disk in which the data was stored. Alternatively, the GPPN 239 may beunmapped, for example, by setting the GPPN 239 to map to a null value.As yet another alternative, the GPPN 239 may be set to map to a common(or “shared”) PPN. By reducing the number of PPNs to which GPPNs map,PPNs may be freed and made available for allocation to other virtualmachines.

In the example illustrated, the GPPN 239 is unmapped. FIG. 5Billustrates an embodiment referred to herein as a “unmapping method,”because the GVPNs allocated to the balloon application 261 map to GPPNswhich are not mapped to a valid PPN. Other examples of unmapping methodswill be described herein with reference to FIGS. 10-12.

As the GPPNs do not map to valid PPNs, the PPN to which the GPPN 239previously mapped can be reclaimed by the kernel and assigned to anothervirtual machine. For example, PPN 132A, which was previously mapped toby GPPN 232A, can now be assigned to virtual machine 200B. As shown inthe figure, GPPN 233B now maps to PPN 132A. Another PPN, not labeled, isnot mapped to by any GPPN, and is available for allocation or use by theresource scheduler 601. Thus unmapping methods are useful for reclaimingguest physical memory, and freeing physical memory for other uses.

FIG. 5C illustrates another embodiment for the reclaiming of physicalmemory allocated to a virtual machine. As in FIG. 5B, some GVPNsallocated to the balloon application 261 map to GPPN 239. However, inthe example illustrated in FIG. 5C, the GPPN 239 maps to a sharedlocation in memory, PPN 134. FIG. 5C illustrates an embodiment referredto herein as a “page-sharing method,” because the GVPNs allocated to theballoon application 261 map to GPPNs that map to a shared PPN. Otherexamples of page-sharing methods will be described herein with referenceto FIGS. 7-9. Additional methods for page-sharing applicable to thepresent invention are described in “Content-Based, Transparent Sharingof Memory,” U.S. Pat. No. 6,789,156, to Carl Waldspurger, which ishereby incorporated by reference in its entirety.

As the GPPNs map to a shared PPN 134, the PPN to which the GPPN 239previously mapped can be reclaimed by the kernel and assigned to anothervirtual machine. For example, PPN 132A, which was previously mapped toby GPPN 232A, can now be assigned to virtual machine 200B. As shown inthe figure, GPPN 233B now maps to PPN 132A. Thus the page-sharingmethods are useful for reclaiming memory in the guest physical memoryaddress space, freeing up memory in the physical memory address spacefor other uses.

FIG. 5D illustrates reallocating physical memory to a virtual machine inresponse to the paging-out of guest virtual memory allocated to aresource request application. As the balloon application is allocatedguest virtual memory, the guest operating system 220A may page-out thevirtual memory allocated to the balloon application. The guest operatingsystem 220A may page-out the guest virtual memory allocated to theballoon application, for example, if it appears the guest virtual memoryhas not been accessed recently, if guest physical memory is tightlyconstrained, or if another application begins accessing large amounts ofguest virtual memory.

In the example illustrated, the guest operating system 220A haspaged-out the guest virtual memory allocated to the balloon application.The GVPNs of the balloon application now map to the virtual disk 240A.The GPPNs to which the GVPNs of the balloon application previouslymapped are now mapped to by some other GVPNs. According to oneembodiment of the present invention, the resource scheduler 601 mapsthese GPPNs to PPNs in preparation for their use by other guestapplications, or by the guest operating system 220A.

When physical memory is constrained, memory management is an ongoingtask. In the example illustrated, the balloon application was used toreclaim guest physical memory from virtual machine 200A. The resultingfree physical memory could then be assigned to virtual machine 200B.However, when the guest operating system 220A paged-out the balloonapplication's allocation, the guest physical memory of virtual machine200A effectively reexpanded. Though not illustrated, a subsequent stepof the resource scheduler 601 might be an attempt to reclaim physicalmemory from other virtual machines, for example, by executing a balloonapplication in the virtual machine 200B. Thus a group of balloonapplications could be used to cooperatively allocate and reclaimphysical memory for various virtual machines in an ongoing cycle of giveand take, and the physical memory requirements of the various virtualmachines could be dynamically accommodated.

FIG. 6 illustrates a method used by a resource request application,according to one embodiment of the present invention. According to oneembodiment of the present invention, the method is used by the balloonapplication 261.

The balloon application 261 receives 602 allocation parameters. Theallocation parameters describe the memory allocation to be requested bythe balloon application 261. For example, the allocation parameters caninclude the size of the guest virtual memory allocation to be requestedby the balloon application 261. According to one embodiment of thepresent invention, the allocation parameters further describe thefrequency with which the balloon application 261 should access theallocated guest virtual memory. According to one embodiment of thepresent invention, the allocation parameters are sent from the resourcescheduler 601, which is described in further detail herein withreference to FIG. 13.

The balloon application 261 requests 604 a memory allocation. Accordingto one embodiment of the present invention, the balloon application 261requests 604 a memory allocation, as described by the allocationparameters, from the guest operating system 220. The guest operatingsystem 220 allocates a location in guest virtual memory described by atleast one GVPN. According to one embodiment of the present invention,the balloon application 261 requests 604 multiple memory allocations.

According to one embodiment of the present invention, the balloonapplication 261 adjusts 604 a memory allocation. Adjusting 604 a memoryallocation can include increasing or reducing the memory allocation ofthe balloon application 261. For example, if the allocation parametersindicate that the memory allocation of the balloon application 261should be smaller, adjusting 604 a memory allocation can include freeinga guest virtual memory allocation previously allocated by the guestoperating system 220. In many guest operating systems 220, guest virtualmemory can be allocated and freed using memory management calls, such asmmap( ) munmap( ) mremap( ), malloc( ), free( ) or other library calls.These examples have been given for the purposes of illustration and arenot limiting. Other various calls may be used in various operatingsystems without departing from the present invention.

The balloon application 261 optionally sends 606 the GVPN of theallocated guest virtual memory to the balloon application driver 225.According to one embodiment of the present invention, the balloonapplication 261 sends the GVPNs of the allocated guest virtual memory tothe balloon application driver 225 to facilitate the efficientreclaiming of physical memory. According to one embodiment of thepresent invention, either instead of or in addition to sending 606 theGVPN of the allocated guest virtual memory to the balloon applicationdriver 225, the balloon application 261 writes a predetermined value tothe memory location (or range of memory locations) described by theGVPN. For example, the balloon application 261 can write a series ofpredetermined values to fill the page described by the GVPN. Writing apredetermined value to the page given by the GVPN is particularly usefulin conjunction with a page-sharing method for guest physical memoryreclamation, several of which are described herein with reference toFIGS. 7-9.

The balloon application 261 delays 607. According to one embodiment ofthe present invention, the balloon application 261 delays 607 for apredetermined amount of time. According to one embodiment of the presentinvention, the balloon application 261 delays 607 for an amount of timethat is responsive to the received 602 allocation parameters.

The balloon application 261 optionally accesses 608 the allocated guestvirtual memory. Accessing 608 the allocated guest virtual memory causesthe guest operating system 220 to perceive that the GVPN of theallocated guest virtual memory is active, and furthermore, to page-inthe allocated guest virtual memory if it has been previously paged-outby the guest operating system 220. According to one embodiment of thepresent invention, the balloon application 261 accesses only a subset ofthe allocated guest virtual memory. The amount of the allocated guestvirtual memory to access can be prescribed, for example, in theallocation parameters.

The balloon application 261 determines 610 if the guest virtual memoryallocation should be adjusted. According to one embodiment of thepresent invention, the balloon application 261 determines 610 if theguest virtual memory allocation should be adjusted by polling theballoon application driver 225. If the balloon application 261determines that the guest virtual memory allocation should not beadjusted, the balloon application 261 returns to delay 607. If theballoon application 261 determines that the guest virtual memoryallocation should be adjusted, the balloon application 261 returns torequest 604 an updated memory allocation, requesting either theallocation of additional memory, or the deallocation of memory which hadbeen previously allocated, as is appropriate.

For the purposes of illustration, the method has been showing using apolling implementation for communication between the balloon application261 and the balloon application driver 225. According to anotherembodiment of the present invention, the balloon application 261operates in a loop until it is receives new allocation parameters. Forexample, the balloon application 261 may block indefinitely on the delay607 until the balloon application driver 225 wakes up the balloonapplication 261. As another example, the balloon application 261 mayloop indefinitely over the delay 607 and the access 608, continuouslyaccessing 608 the allocated guest virtual memory according to thefrequency specified in the received 602 allocation parameters, until theballoon application driver 225 sends new allocation parameters to theballoon application 261. As yet another example, allocation parametersmay be sent from the virtual machine monitor or the resource scheduler601 through shared memory. Other methods will be apparent to one ofskill in the art without departing from the scope of the presentinvention.

By executing a resource reservation application running as a guestapplication on the virtual machine, guest virtual memory may be reservedand effectively controlled by the virtualization layer. Manipulatingguest virtual memory facilitates the effective management and allocationof physical memory to virtual machines.

According to one embodiment of the present invention, a plurality ofballoon applications execute as guest applications 260 on the virtualmachine 200. Executing multiple balloon applications is often useful forcircumventing limitations imposed by the guest operating system 220. Forexample, some guest operating systems may organize the guestapplications 260 into several partitions. Executing balloon applicationsin the various partitions allows for determining the memory conditionsof the various partitions. As another example, in some computer systems,such as x86 computer systems using Physical Address Extension (PAE), aguest virtual memory address space may be smaller than the guestphysical address space, thereby limiting the amount of guest virtualmemory any one application can reserve. Executing multiple balloonapplications facilitates the reservation of larger amount of guestvirtual memory

FIG. 7 illustrates a method for supporting a resource reservationapplication, according to one embodiment of the present invention.According one embodiment, the method is performed by the balloonapplication driver 225. FIG. 7 illustrates a method useful inconjunction with a page-sharing method, such as one of those describedherein with reference to FIG. 5C.

The balloon application driver 225 sends 701 allocation parameters tothe balloon application 261. As described previously, the allocationparameters may contain prescriptions for the guest virtual memoryallocation such as allocation size and frequency of allocation accesses.According to one embodiment of the present invention, the balloonapplication driver 225 sends 701 allocation parameters received from theresource scheduler 601.

The balloon application driver 225 optionally receives 702 a GVPNdescribing the guest virtual memory allocated to the balloon application261. The balloon application driver 225 determines the GPPN to which theGVPN is mapped, for example, by calling to the guest operating system220 for translation of the GVPN.

The balloon application driver 225 optionally sends 704 the GPPN to theresource scheduler 601. According to one embodiment of the presentinvention, the GVPN is mapped to a GPPN, and this GPPN is sent from theballoon application driver 225 to the resource scheduler 601. Such atechnique may beneficially improve the efficiency with which the GPPNallocated to the balloon application 261 is identified and increase theability of the resource scheduler 601 to free physical memory allocatedto the virtual machine.

The balloon application driver 225 optionally sets an access bit 706 inthe page table entry mapping the GVPN to the GPPN. Many guest operatingsystems 220 use the access bit of the page table entry to determinewhich pages are active and which are inactive. For example, reading froma virtual memory location associated with a GVPN will set the access bitin the page table entry mapping the GVPN to the GPPN. According to oneembodiment of the present invention, the balloon application driver 225sets the access bit 706 directly, avoiding the step of reading from thevirtual memory location. According to another embodiment of the presentinvention, the access bits are set by the virtual machine monitor. Bysetting the access bit 706, the guest operating system 220 will perceivethat the GVPN is active. The frequency with which the access bit is set706 may be determined by the allocation parameters.

The balloon application driver 225 determines 708 if the allocationparameters should be adjusted. For example, the balloon applicationdriver 225 may determine 708 if the allocation parameters should beadjusted based on communication with the resource scheduler 601. If theballoon application driver 225 determines 708 that the allocationparameters should be adjusted, the balloon application driver 225returns to send 701 the new application parameters to the balloonapplication 261. If the balloon application driver 225 determines 708that the allocation parameters should not be adjusted, the balloonapplication driver 225 returns to set the access bit 706.

For the purposes of illustration, the balloon application driver 225 hasbeen shown as operating in a polling method in which the balloonapplication driver 225 periodically consults the resource scheduler 601to determine 708 if the allocation parameters should be adjusted.According to one embodiment of the present invention, the balloonapplication driver 225 operates in an interrupt driven method. Theballoon application driver 225 receives virtual interrupts sent by theresource scheduler 601 and handles those interrupts appropriately. Forexample, upon receiving a virtual interrupt, the balloon applicationdriver 225 receives new allocation parameters by reading from a guestphysical memory location or by executing input/output instructions.

Executing a device driver, such as the balloon application driver 225,facilitates communication between the balloon application and theresource scheduler, for example. Furthermore, the balloon applicationdriver 225 may improve the efficiency of the balloon application 261 byproviding virtualized accesses to allocated virtual memory.

FIG. 8 illustrates a method managing memory resources, according to oneembodiment of the present invention. In one embodiment, the method isperformed by the resource scheduler 601. FIG. 8 illustrates a methoduseful in conjunction with a page-sharing method, such as one of thosedescribed herein with reference to FIG. 5C.

The resource scheduler 601 receives 802 a GPPN. The received 802 GPPN ismapped to by a GVPN. According to one embodiment of the presentinvention, the resource scheduler 601 determines if the GVPN (which mapsto the received 802 GPPN) is allocated to the balloon application 261 bysearching the page for predetermined data. The search for pagescontaining predetermined data may be performed by iterating over GPPNs.For example, the resource scheduler 601 receives 802 a GPPN from thevirtual machine monitor. The resource scheduler 601 determines the PPNto which the GPPN is mapped. The resource scheduler 601 reads data fromthe physical page, and determines if the physical page containspredetermined data (for example, the predetermined data written to thepage by the balloon application 261.) If the resource scheduler 601determines that the physical page does not contain a predeterminedvalue, the resource scheduler 601 returns to receives 802 another GPPNfrom the virtual machine monitor. If the resource scheduler 601determines that the physical page does contain a predetermined value,the resource scheduler 601 has identified a GPPN associated with a GVPNallocated to the balloon application 261. By searching for predeterminedvalues in memory, the resource scheduler 601 may identify guest physicalpages eligible for page sharing without the explicit transfer of GPPNs.

According to another embodiment of the present invention, a GPPNassociated with a GVPN allocated to the balloon application 261 is sentto the resource scheduler 601 by the balloon application driver 225.(See step 704 described herein with reference to FIG. 7.) The resourcescheduler 601 receives a GPPN from the balloon application driver 225(possibly through the assistance of the virtual machine monitor). Theresource scheduler 601 determines the PPN to which the GPPN is mapped,for example, using the page table (or another page mapping datastructure) maintained by the resource scheduler 601. By receiving a GPPNassociated with a GVPN allocated to the balloon application 261, theresource scheduler 601 may efficiently identify physical pages eligiblefor page sharing.

The resource scheduler 601 maps 804 the GPPN to a shared PPN. Forexample, the resource scheduler 601 can change the page table entry forthe GPPN so that it no longer maps to the PPN determined in 802 andinstead maps to a second PPN. This second PPN may be shared in the sensethat is has multiple GPPNs mapping to it. Alternatively, this second PPNmay only have a single GPPN mapping to it, but is eligible to have otherGPPNs map to it in subsequent mappings 804.

The resource scheduler 601 frees 806 the first PPN. As the GPPN nolonger maps to the first PPN, the first PPN can be marked as unused andenter a pool of available PPNs. The resource scheduler 601 optionallyallocates 807 the freed PPN to a different virtual machine.Alternatively, the resource scheduler 601 can use the freed PPN forother purposes. For example, the resource scheduler 601 can store kerneldata in the memory designated by the freed PPN, or can maintain theavailable physical memory for future use.

As long as the guest operating system 220 maps the balloon application'sGVPN to the same GPPN, the mapping of the GPPN to the shared PPN willbeneficially reduce the total amount of physical memory used by thevirtual machine. However, as the balloon application 261 is allocatedguest virtual memory, it is the possible that the guest operating system220 may page-out the balloon application's guest virtual page and mapsome other GVPN to the GPPN. This other guest virtual page may beallocated to another guest application, or to the guest operating systemitself. From the point of view of the resource scheduler 601, thisarrangement is acceptable up until the time that the other guestapplication attempts to write to the GVPN mapped to the GPPN.

The resource scheduler 601 traps 808 the attempt to write to the GPPN.According to one embodiment of the present invention, the resourcescheduler 601 may trap 808 the attempt to write the GPPN by marking theGPPN as read-only in the hardware page table, which will cause anexception when a write is attempted.

The resource scheduler 601 maps 810 the GPPN to an available PPN. Theavailable PPN may be taken from a pool of available physical memorypages, or it may be acquired by mapping some other GPPN to a shared PPN.The GPPN is mapped to an available PPN, and the write to that GPPN maycontinue as normal.

At some time later, the balloon application's GVPN will be paged-in. Forexample, the balloon application 261 may be periodically accessing theguest virtual memory allocated to it, and if the GVPN is paged-out thisaccess will create a page fault, causing the guest operating system 220to page-in the GVPN and map it to a (possibly different) GPPN. Accordingto one embodiment of the present invention, the balloon applicationdriver 225 will detect the page-in and send the new GPPN to the resourcescheduler 601. According to another embodiment of the present invention,the page-in by the guest operating system 220 will copy thepredetermined data (previously written by the balloon application 261)to a new location in memory. The resource scheduler 601 returns toreceiving 802 a GPPN, and begins the process of freeing another PPN as aresult of the page-in of the balloon application's guest virtual page.

FIG. 9 illustrates a method for reclaiming physical memory assigned to avirtual machine, according to one embodiment of the present invention.FIG. 9 shows an exemplary timeline along with steps performed by theguest operating system 220 and the resource scheduler 601 in one of theembodiments employing a page-sharing method.

The guest operating system 220 allocates 903 a GVPN and maps the GVPN toa GPPN. Typically the guest operating system 220 allocates 903 a GVPN inresponse to a request for memory by the balloon application 261. Theguest operating system 220 may map the GVPN to a GPPN either upon theallocation of the GVPN, or in response to a write or read to the GVPN.The GPPN in turn is mapped to a first PPN.

According to one embodiment of the present invention, the balloonapplication 261 writes a predetermined value into the memory location(or range of memory locations) described by the GVPN. For example, theballoon application 261 can write a series of predetermined values tofill the page described by the GVPN.

The resource scheduler 601 finds 906 the GPPN to which the GVPN ismapped. According to one embodiment of the present invention, theresource scheduler 601 finds 906 the GPPN to which the GVPN is mapped bysearching for a predetermined value. The resource scheduler 601 thenmodifies (for example, with the help of the virtual machine monitor 300)the page table mapping of that GPPN, and maps the GPPN to a shared(second) PPN. The first PPN is then free, and can be assigned to othervirtual machines.

The guest operating system 220 pages-out 907 the GVPN allocated to theballoon application 261. A different GVPN, possibly not allocated to theballoon application 261, is now mapped to the GPPN.

The guest operating system 220 writes 909 to the GPPN.

The resource scheduler 601 traps 910 the write to the GPPN, and breaksthe mapping of the GPPN to the shared (second) PPN. An available PPN (athird PPN) is allocated to the virtual machine, and the GPPN is mappedto the third PPN. According to one embodiment of the present invention,the resource scheduler 601 blocks the virtual machine until a PPN can bereclaimed and allocated. After the GPPN has been mapped to the thirdPPN, the guest operating system 220 write to the GPPN continues asnormal.

The guest operating system 220 pages-in 911 the GVPN allocated to theballoon application 261. The GVPN is now mapped to a (possiblydifferent) GPPN. The resource scheduler 601 finds 912 the GPPN to whichthe GVPN allocated to the balloon application 261 is mapped. Theresource scheduler 601 then modifies (for example, with the help of thevirtual machine monitor 300) the page table mapping of that GVPN, andchanges the mapping from the third PPN to a shared PPN. The third PPN isthen free, and can be assigned to other virtual machines. Thus thepattern may continue, with the guest operating system 220 repeatedlypaging-in and paging-out the guest virtual memory allocated to theballoon application 261, and the resource scheduler 601 repeatedlyfreeing PPNs containing predetermined values and trapping writes to theshared PPN. By consolidating physical pages containing the same data,physical memory usage may be reduced. Therefore, writing predetermineddata into memory allocated to the balloon application 261 facilitatesthe reclaiming of physical memory assigned to a virtual machine.

FIG. 10 illustrates a method for supporting a resource reservationapplication, according to another embodiment of the present invention.According one embodiment, the method is performed by the balloonapplication driver 225. FIG. 10 illustrates a method useful inconjunction with an unmapping method, such as one of those describedherein with reference to FIG. 5B.

The balloon application driver 225 sends 1001 allocation parameters tothe balloon application 261. The allocation parameters sent 1001 by theballoon application driver 225 may be received from the resourcescheduler 601. As in the method described herein with reference to FIG.7, the balloon application driver 225 may serve as a communication linkbetween the balloon application 261 and the resource scheduler 601.

The balloon application driver 225 receives 1002 the GVPN of the guestvirtual memory allocated to the balloon application 261, and determinesthe GPPN to which it is mapped. According to one embodiment of thepresent invention, the balloon application driver 225 receives a GVPNfrom the balloon application 261, and calls to the guest operatingsystem 220 to deference the GVPN to a GPPN. According to anotherembodiment of the present invention, the virtual machine monitortranslates the GVPN to a GPPN by consulting the guest page table.

The balloon application driver 225 sends 1004 the GPPN (to which thereceived 1002 GVPN is mapped) to the resource scheduler 601.

The balloon application driver 225 optionally sets an access bit 1006 inthe page table entry mapping the GVPN to the GPPN. Many guest operatingsystems 220 use the access bit of the page table entry to determinewhich pages are active and which are inactive. For example, reading froma virtual memory location associated with a GVPN will set the access bitin the page table entry mapping the GVPN to the GPPN. According to oneembodiment of the present invention, the balloon application driver 225sets the access bit 1006 directly, avoiding the step of reading thevirtual memory allocation. By setting the access bit 1006, the guestoperating system 220 will perceive that the GVPN is active. Thefrequency with which the access bit is set 1006 may be determined by theallocation parameters.

According to one embodiment of the present invention, the balloonapplication driver 225 sets a dirty bit in the page table entry mappingthe GVPN to the GPPN. A dirty bit indicates to the guest operatingsystem 220 that the page has been modified since it was last written todisk. Thus setting the dirty bit will cause the guest operating system220 to copy the page to disk when paging out the GVPN, assisting theballoon application driver 225 with detection of the page-out.

The balloon application driver 225 determines 1008 if the guestoperating system 220 has paged-out the balloon application's GVPN. Theballoon application driver 225 can determine 1008 if the guest operatingsystem 220 has paged-out the balloon application's GVPN using a varietyof methods. For example, when the balloon application 261 requests theguest virtual memory from the guest operating system 220, it mayindicate to the guest operating system 220 that the memory is associatedwith a device (real or virtual) for which the balloon application driver225 is responsible. In this case, many guest operating systems 220 willcall to the balloon application driver 225 when paging-out the virtualmemory, providing the balloon application driver 225 with theopportunity to catch the page-out. In some cases, the balloonapplication driver 225 will be responsible for performing the page-out,advantageously increasing efficiency by avoiding costly virtual diskwrites by the guest operating system 220.

The virtualization layer has other opportunities in which to trap thepage-out by the guest operating system 220. For example, typically thevirtual machine monitor will be involved in accesses to the virtual disk240. As the guest operating system 220 will typically page-out guestvirtual memory using the virtual disk 240, the virtual machine monitormay catch the write to the virtual disk 240 and notify the balloonapplication driver 225 of the page-out.

As yet another alternative, the virtual machine monitor may mark asread-only the memory containing the page table mapping the GVPNs toGPPNs. Page-outs will typically involve a change to this page table bythe guest operating system 220. When the guest operating system 220attempts to write to the page table, the virtual machine monitor cantrap the write and notify the balloon application driver 225 of thepage-out. According to another embodiment of the present invention, thevirtual machine monitor can trap a write to the page table and notifythe resource scheduler 601 directly.

These examples of methods for determining if the guest operating system220 has paged-out the guest virtual memory allocated to the balloonapplication 261 are given for the purposes of illustration only and arenot limiting. Other methods for determining page-out by the guestoperating system 220 will be apparent to one of skill in the art withoutdeparting from the scope of the present invention.

If the balloon application driver 225 determines 1008 that the guestoperating system 220 has not paged-out the GVPN allocated to the balloonapplication 261, the balloon application driver 225 optionally returnsto setting the access bit 1006.

If the balloon application driver 225 determines 1008 that the guestoperating system 220 has paged-out the GVPN allocated to the balloonapplication 261, the balloon application driver 225 notifies theresource scheduler 601 that the GVPN allocated to the balloonapplication 261 has been paged-out. For the purposes of illustration theballoon application driver 225 is shown as notifying 1009 the resourcescheduler 601 in response to the determination 1008, but according toone embodiment of the present invention the resource scheduler 601 maywait until the guest operating system 220 attempts to write to the GPPNbefore notifying 1009 the resource scheduler 601.

The resource scheduler 601 optionally requests 1010 page-in of the GVPN.The resource scheduler 601 can request 1010 page-in of the GVPN, forexample, by calling to the guest operating system 220 for translation ofthe GVPN. If the GVPN is paged-out, this call will typically result in apage fault, and the guest operating system 220 will page-in the GVPN.

According to one embodiment of the present invention, the resourcescheduler 601 requests 1010 page-in of the GVPN according to a frequencydescribed in the allocation parameters. According to another embodimentof the present invention, the resource scheduler 601 requests 1010page-in of the GVPN in response to determining 1008 that the guestoperating system 220 has paged-out the GVPN. According to yet anotherembodiment of the present invention, the resource scheduler 601 requests1010 page-in of the GVPN in response to a command from the resourcescheduler 601. By explicitly calling to the guest operating system 220for a page-in, the balloon application driver 225 may control thefrequency of the page-in of the GVPN.

FIG. 11 illustrates a method for managing memory resources, according toanother embodiment of the present invention. In one embodiment, themethod is performed by the resource scheduler 601. FIG. 11 illustrates amethod useful in conjunction with an unmapping method, such as one ofthose described herein with reference to FIG. 5B.

The resource scheduler 601 receives 1102 a GPPN. According to oneembodiment of the present invention, the resource scheduler 601 receives1102 a GPPN from the balloon application driver 225. The resourcescheduler 601 determines the PPN to which the GPPN is mapped, forexample, by consulting the page table (or another page mapping datastructure) maintained by the resource scheduler 601.

The resource scheduler 601 frees 1104 the PPN to which the GPPN ismapped. For example, the resource scheduler 601 can allocate the PPN toa pool of available PPNs. According to one embodiment of the presentinvention, the resource scheduler 601 can assign the PPN to anothervirtual machine.

According to one embodiment of the present invention, the resourcescheduler 601 changes the page table entry for the GPPN to indicate thatthe GPPN is unmapped. For example, the resource scheduler 601 can mapthe GPPN to map to a predetermined invalid PPN, can set the GPPN so thatit does not map to any PPN, or can set a separate field indicating thatthe GPPN is not mapped to a PPN. Other methods will be apparent to oneof skill in the art without departing from the scope of the presentinvention.

The resource scheduler 601 waits 1106 for an indication that the GVPNallocated to the balloon application 261 has been paged-out. Forexample, the resource scheduler 601 can wait 1106 for an indication thatthe GVPN has been paged-out by periodically polling the balloonapplication driver 225, or by waiting for a message from the balloonapplication driver 225 indicating a page-out of the GVPN.

The resource scheduler 601 maps 1108 the GPPN to an available PPN.According to one embodiment of the present invention, the resourcescheduler 601 allocates an available PPN and changes the page tableentry for the GPPN to map to the allocated PPN. Typically, the resourcescheduler 601 maps 1108 the GPPN to an available PPN when the guestoperating system 220 has paged-out the GVPN allocated to the balloonapplication 261. When some other GVPN (potentially allocated to someother guest application) is mapped to the GPPN, the GPPN is mapped to anavailable PPN. The memory allocation will thus behave as expected by theother guest application. The resource scheduler 601 returns to receives1102 another GPPN.

For the purposes of illustration, the resource scheduler 601 has beenshown to allocate an available PPN in response to the GVPN page-outmessage from the balloon application driver 225. According to anotherembodiment of the present invention, an available PPN is allocated inresponse to a detected attempt to write to a GPPN (for example, as shownin FIG. 9), and the GVPN page-out message is used to determine memoryconditions inside the virtual machine.

By freeing and allocating physical pages as directed by the balloonapplication driver 225, the resource scheduler 601 facilitates thereclaiming of physical memory from virtual machines while at the sametime ensuring that the memory allocations of other guest applicationsbehave normally.

According to another embodiment of the present invention, the resourcescheduler 601 maps 1108 to an available PPN in response to detecting anattempted write (for example by the guest operating system 220 or byanother guest application) to the GPPN.

FIG. 12 illustrates a method for reclaiming physical memory, accordingto another embodiment of the present invention. FIG. 12 shows anexemplary timeline along with steps performed by the guest operatingsystem 220, the balloon application driver 225, and the resourcescheduler 601 in one of the embodiments employing an unmapping method.

The guest operating system 220 allocates 1214 a GVPN and maps the GVPNto a GPPN. Typically the guest operating system 220 allocates 1214 aGVPN in response to a request for memory by the balloon application 261.The guest operating system 220 may map the GVPN to a first GPPN eitherupon the allocation of the GVPN, or in response to a write or read tothe GVPN by the balloon application 261. The first GPPN in turn ismapped to a PPN.

The balloon application driver 225 determines 1216 the GPPN to which theGVPN is mapped. The balloon application driver 225 sends the GPPN to theresource scheduler 601.

The resource scheduler 601 determines 1218 the PPN to which the GPPN ismapped, and frees the PPN. The physical memory allocated to the virtualmachine is reclaimed, and the PPN may then be assigned to anothervirtual machine, or may enter a pool of available PPNs.

The guest operating system 220 pages-out 1220 the GVPN allocated to theballoon application 261. A different GVPN, possibly not allocated to theballoon application 261, may now be mapped to the GPPN. (In someoperating systems, the GVPN may still map to the GPPN although the GVPNhas been paged out. However, when the GPPN is later used for anotherpurpose this mapping will be modified.)

The balloon application driver 225 traps 1222 the page-out, and sends aGVPN page-out notice to the resource scheduler 601. According to oneembodiment of the present invention, the resource scheduler 601 includesthe GPPN to which the paged-out GVPN was previously mapped.

The resource scheduler 601 allocates 1224 a PPN (either from a pool ofavailable PPNs or by reclaiming a PPN allocated to another virtualmachine) and maps the GPPN to the PPN. According to one embodiment ofthe present invention, the resource scheduler 601 blocks the virtualmachine until a PPN can be reclaimed and allocated.

The guest operating system 220 pages-in 1226 the GVPN allocated to theballoon application 261. The GVPN is now mapped to a second GPPN,although potentially this GPPN is different than the first GPPN.

The balloon application driver 225 determines 1228 the GPPN to which theGVPN is mapped. According to one embodiment of the present invention,the balloon application driver 225 determines 1228 the GPPN to which theGVPN is mapped by observing the page table modifications performed bythe guest operating system 220. According to another embodiment of thepresent invention, the virtual machine monitor (or another component ofthe virtualization layer) determines the GPPN to which the GVPN ismapped by observing the page table modifications performed by the guestoperating system 220, and sends the GPPN to the resource scheduler 601.

The resource scheduler 601 determines 1230 the PPN to which the GPPN ismapped, and frees the PPN. The PPN may then be assigned to anothervirtual machine, or may enter a pool of available PPNs. Thus the cyclecan repeat, with the guest operating system 220 periodically page-in andpaging-out the GVPN allocated to the balloon application 261, and theresource scheduler 601 repeatedly freeing and allocating PPNs inresponse to messages from the balloon application driver 225. Byunmapping GPPNs in use by the balloon application 261, physical pagesmay be reclaimed from the virtual machine. Furthermore, allocatingphysical pages when the balloon application's allocation is paged-outfacilitates the continued functionality of the virtual machine.

Several page-sharing and unmapping methods have been presented herein inconjunction with various embodiments for the purpose of illustration.These examples have been selected for the purposes of illustration andare not limiting. When various optional elements and features aredescribed as applicable to certain methods, it is presumed that theseelements and features may also be applicable to other methods describedherein. One of skill in the art will recognize various usefulcombinations of the elements, methods, and embodiments described hereinwithout departing from the scope of the present invention.

FIG. 13 illustrates a method for determining memory conditions inside avirtual machine. According to one embodiment of the present invention,the method is performed by the resource scheduler 601.

By using a balloon application 261 to reserve guest virtual memory, theresource scheduler 601 is capable of determining memory conditionsinside the virtual machine. The allocation to the balloon application261 is subject to page-out and page-in by the guest operating system220. The rate and manner of the page-out and page-in of the allocationare typically reflective of memory conditions inside the virtualmachine, and in many cases are useful for insight into the memorymanagement operations being performed by the guest operating system 220.Thus the balloon application 261 is further useful for determiningmemory conditions inside the virtual machine. The balloon application261 receives an allocation of guest virtual memory, and is then subjectto the winds and currents of the paging operations of the guestoperating system 220—much the same as other guest applications. Testingand understanding memory conditions inside the virtual machine, as wellas witnessing the paging practices of the guest operating system 220firsthand, can improve the effectiveness of the allocation of resourcesto the various virtual machines.

The resource scheduler 601 sets 1302 allocation parameters for theballoon application 261. According to one embodiment of the presentinvention, the allocation parameters provide a plurality of inputs withwhich the resource scheduler 601 can manipulate the balloon application261. For example, according to one embodiment of the present invention,a balloon application 261 requests a guest virtual memory allocation ofa size indicated by allocation parameters. As another example, accordingto one embodiment of the present invention, the balloon application 261accesses its guest virtual memory allocation with a frequency indicatedby the allocation parameters. According to another embodiment, theballoon application driver 225 periodically sets access bits for one ormore of the GVPNs allocated to the balloon application 261, and thefrequency of these accesses is indicated by the allocation parameters.

According to one embodiment of the present invention, the allocationparameters include an average frequency with which the balloonapplication 261 or balloon application driver 225 should access thememory allocation. When the allocation parameters specify an averageaccess frequency, the frequency with which the memory location isaccessed can vary from access to access in some interval around theaverage frequency. Varying the access frequency can beneficially avoidunintentional correlation with periodic operations inside the guestoperating system 220, and can provide more consistent paging performanceand smoother measurements of memory conditions inside the virtualmachine

According to one embodiment of the present invention, the allocationparameters specify different access frequencies for different memoryregions. For example, the balloon application driver 225 may beinstructed to access certain portions of its allocation with a firstfrequency and other portions of its allocation with a second frequency.The allocation parameters can specify any number of frequencies for anynumber of different memory regions, and for any number of balloonapplications. By specifying different access frequencies for differentmemory regions, the resource scheduler 601 can determine the memoryconstraint of the virtual machine on various time scales, providinggreater insight into the memory conditions of the virtual machine.

The resource scheduler 601 measures 1304 the frequency of the page-outof the balloon application's guest virtual memory allocation anddetermines memory conditions inside the virtual machine. In oneembodiment, the frequency of page-out is measured 1304 by measuring aninterval between causing a guest virtual location to be mapped to aguest physical location and determining that the guest virtual locationis not mapped to the guest physical location. The interval can bemeasured in a variety of units. For example, the interval can bemeasured in seconds, physical CPU cycles, virtual CPU cycles, virtualmachine execution time, or in terms of any other number of observableevents. In one embodiment, an event counter is included to perform theinterval measurement.

Frequent page-out of the guest virtual memory allocation may indicatethat memory resources are tightly constrained inside the virtualmachine. Frequent page-out of a small virtual machine allocation that isbeing frequently accessed would indicate a particularly stressed memorysituation. On the other hand, infrequent page-out of the guest virtualmemory allocation may indicate that memory resources are more available.Infrequent page-out of a large virtual machine allocation that is beingaccessed only rarely would indicate a particularly relaxed memorysituation. Depending on the guest operating system 220, the frequency ofpage-out of memory allocations resulting from various combinations ofallocation parameters may have different interpretations with respect tomemory conditions inside the virtual machine.

The frequency with which a page is paged-out is closely related to theage of that page at the time of page-out. The age of a page at page-outis a measurement of the amount of time between the last detected accessto that page and the paging-out of the page. Under relaxed memoryconditions, the pages that are paged-out will tend to be very old, aspage-outs occur infrequently and even relatively inactive pages willtypically be allowed to remain in memory. As memory becomes moreconstrained, however, the guest operating system will page-out morefrequently, and will typically page-out younger and younger pages.

By detecting page-out of pages allocated to the balloon application 261,the resource scheduler 601 can estimate the average age of pages beingpaged-out by the guest operating system 220. For example, the resourcescheduler 601 can perform a moving average, or any other statisticalcharacterization, of the age of the pages allocated to the balloonapplication 261 at page-out, and use this data to estimate memoryconditions in the virtual machine. Furthermore, the resource scheduler601 can set multiple access frequencies for the balloon application 261and observe which pages are paged-out to determine the approximate ageof pages being paged-out by the guest operating system 220. The age ofpages being paged-out is a useful metric, for example, for comparingmemory conditions among multiple virtual machines. Other examples ofmetrics of memory conditions inside the virtual machine that can beusefully estimated using a balloon application will be apparent to oneof skill in the art without departing from the scope of the presentinvention.

The resource scheduler 601 can further determine memory conditions inthe virtual machine by observing how the age of pages being paged-outchanges in response to manipulations of the size and/or access frequencyof the allocation to the balloon application.

The resource scheduler 601 can use the determined memory conditions fora variety of purposes. According to one embodiment of the presentinvention, the resource scheduler 601 optionally adjusts 1306 theallocation of physical memory to the various virtual machines inresponse to the determined 1304 memory conditions. For example, avirtual machine demonstrating a particularly tight memory situation mayhave the effective size of its guest physical memory expanded, while avirtual machine demonstrating a relaxed memory situation may have theeffective size of its guest physical memory reduced. The allocation ofphysical memory to the various virtual machines may be adjustedaccordingly. The allocation of physical memory to the various virtualmachines can be adjusted by paging-out guest physical memory to anotherstorage device, by use of a balloon application, or by other knownmethods for reclaiming memory from a virtual machine. For example, theallocation of physical memory to the various virtual machines can beadjusted by changing the frequency with which memory allocated to aballoon application is accessed, or by changing the size of the memoryallocated to a balloon application. The resource scheduler 601 returnsto set 1302 allocation parameters for the balloon application 261.

According to another embodiment of the present invention, the determinedmemory conditions are sent to a guest application running in the virtualmachine. The memory conditions may be sent from the resource scheduler601 to a guest application in a variety of ways. For example, the guestapplication can make a hypercall to the virtualization layer, which inturn provides memory condition data to the guest application. As anotherexample, virtual input/output ports on the virtual machine can be usedto transfer memory condition data to a guest application. As yet anotherexample, shared memory can be used to send determined memory conditionsto the guest application. In one embodiment, the guest application towhich determined memory conditions are sent can be the balloonapplication 261.

Furthermore, a first guest application running on the virtual machinecan send the memory condition data to other guest applications. Forexample, a first guest application can be designed to work inconjunction with the virtualization layer to receive memory conditiondata, and this memory condition data can be sent to other guestapplications through a shared function library.

Sending memory condition data to a guest application allows for a moreefficient use of system resources. For example, some guest applications,such as database server applications, maintain large data structures,such as hash tables, that are pinned in guest physical memory. The sizeof these guest data structures typically depends on the size of theguest physical memory. However, as some of the guest physical memorycould have been reclaimed by the resource scheduler 601, the guestapplication may have out of date or faulty data regarding the size ofthe guest physical memory. Sending memory condition data to the guestapplication can improve the effectiveness of the guest data structures.As another example, some guest applications organize their memory spacedifferently under different memory conditions, and have routines forquerying the guest operating system 220 for information about memoryconditions. In a virtual machine, the guest operating system 220sometimes does not have complete information about the true state ofmemory for that virtual machine. Sending memory condition data to theguest application facilitates efficient memory use by the application.

According to yet another embodiment of the present invention, the memorycondition data is stored and analyzed. Memory condition data can provideinsight into the behavior of the guest operating system 220, which isuseful for the development of operating systems and for the improvementof the virtualization layer. For example, analysis of the memorycondition data may show whether the guest operating system 220 ispaging-out pages randomly, or using a technique such as Least RecentlyUsed (LRU) paging. Determining the paging policies of the guestoperating system 220 can facilitate improved virtualization performance.

The resource scheduler and balloon application can also cooperative topush memory conditions in the virtual machine towards some target memoryconditions. The target memory conditions can define conditions in whicha certain level of virtual machine performance can be expected. Forexample, when pages are paged-out too frequently, a computer system canspend a disproportionate amount of time paging-in and paging-out ratherthan making forward computational progress. Such a condition is commonlyreferred to as “thrashing”. In this example, there may be a certainaverage age of paged-out pages below which thrashing occurs and theperformance of the virtual machine suffers significantly. If the averageage of paged-out pages is maintained above that threshold, thrashing maybe avoided and system performance improved. In on example, the targetmemory conditions define conditions that will ordinarily avoidthrashing.

FIG. 14 illustrates a method for controlling memory conditions inside avirtual machine. The resource scheduler 601 sets 1402 a target memorycondition. For example, the resource scheduler 601 may set 1402 a targetaverage age of paged-out pages so that the target is above somethrashing threshold. According to one embodiment of the presentinvention, the target memory condition can be adjusted in response tothe performance of the virtual machine. For example, thrashing cansometimes be detected by monitoring the performance of the virtualhardware, or by querying the guest operating system 220 for performanceinformation. When thrashing is detected, target memory conditions can beadjusted accordingly.

The resource scheduler 601 allocates or reclaims 1404 memory to adjustthe memory allocation to the virtual machine. The resource scheduler 601can adjust the memory allocation to the virtual machine by use of aballoon application, through paging-out of guest physical memory, orusing another method.

Optionally, the resource scheduler 601 delays 1405. For example, theresource scheduler 601 can delay 1405 for some predetermined amount oftime. Delaying beneficially allows the memory state in the virtualmachine to settle before making further determinations.

The resource scheduler 601 determines 1406 a memory condition in thevirtual machine. For example, the resource scheduler 601 can determine1406 the average age of pages paged-out in the virtual machine. Theresource scheduler 601 can determine 1406 a memory condition, forexample, using one of the methods described herein with reference toFIG. 13.

The resource scheduler 601 determines 1408 if the determined memorycondition is within the bounds of the target memory condition. Forexample, the resource scheduler 601 can determine 1408 if the determinedaverage age of pages paged-out in the virtual machine is equal to orabove the target average age of paged-out pages. If the determinedaverage age is equal to or above the target average age, the determinedmemory condition is within the bounds of the target memory condition. Ifthe determined average age is below the target average age, thedetermined memory condition is outside the bounds of the target memorycondition.

The bounds of the target memory condition can be multidimensional, andcan depend on any number of factors. As another example, the resourcescheduler 601 can determine 1408 if the number of pages allocated to theballoon application 261 paged-out by the guest operating system 220 withages below a target age exceeds some target threshold. Other exampleswill be apparent to one of skill in the art without departing from thescope of the present invention.

If the resource scheduler 601 determines 1408 that the determined memorycondition is outside the bounds of the target memory condition, theresource scheduler 601 adjusts the memory allocation to the virtualmachine by returning to allocate or reclaim 1404 memory. For example, ifthe determined average age of pages paged-out in the virtual machine isequal to or below the target average age of paged-out pages, theresource scheduler 601 can allocate additional memory to the virtualmachine. As another example, if the determined memory conditionindicates a large margin above the target memory condition, and memoryresources are in demand from other virtual machines, the resourcescheduler 601 can reclaim memory from the virtual machine.

If the resource scheduler 601 determines 1408 that the determined memorycondition is within the bounds of the target memory condition, theresource scheduler 601 returns to determining 1406 memory conditions inthe virtual machine. According to one embodiment of the presentinvention, the resource scheduler 601 returns to delaying 1405.

Thus the resource scheduler 601 and balloon application 261 can be usedin conjunction to set memory conditions in the virtual machine to atarget memory condition. The target memory condition can be given as anymetric by which memory conditions can be measured. The target memorycondition can be selected to optimize the performance of the virtualmachine, or to optimize the performance of the virtual machine subjectto some constraint, such as the requirements of other virtual machines.Different virtual machines can have different target memory conditions.The resource scheduler 601 can prioritize virtual machines and settarget memory conditions based on these priorities. For example, thetarget age for a low-priority virtual machine may be much lower that thetarget age for a high-priority virtual machine. As another example, theresource scheduler 601 can attempt to set target memory conditions suchthat none of the virtual machines enters a thrashing state, and allocatememory resources accordingly. If satisfying this constraint provesdifficult, the resource scheduler 601 can attempt to set target memoryconditions such that certain high priority virtual machines are kept outof a thrashing state, potentially at the expense of the performance ofother virtual machines. The resource scheduler 601 can then allocatememory resources accordingly. These examples have been given for thepurposes of illustration, but other applications for setting a targetmemory condition in a virtual machine will be apparent to one of skillin the art without departing from the scope of the present invention.

Executing a resource reservation application is a powerful tool inconfigurations having multiple layers of memory addressing. Not only canthe application be used to reclaim memory, but the application cangather information regarding memory conditions in other memory addresslayers. Thus the resource application is a useful tool both forassigning balanced memory allocations and for enacting thoseallocations. Through cooperation on multiple levels of memoryaddressing, more efficient allocation of memory resources may beachieved.

Throughout this description, reference is made to reserving a locationin memory, determining a page number, sending a page number, reclaiminga page number, and so on. In the interest of clarity, variousembodiments of the present invention have been described using singularterminology. Furthermore, when reference is made herein to a “memorylocation,” it will be apparent that, according to various embodiments, amemory location can be a single memory address, a range of memoryaddress, a page, a subset of a page, a range of pages, and so on. Thisterminology has been selected for the purposes of illustration and isnot limiting. The methods described herein may be advantageouslyimplemented as batch processes in which a plurality of locations arereserved, a plurality of page numbers are sent, determined, orreclaimed, and so on. Other techniques for combining various stepsdescribed herein will be apparent to one of skill in the art withoutdeparting from the scope of the present invention.

Reference in the specification to “one embodiment” or to “an embodiment”means that a particular feature, structure, or characteristic describedin connection with the embodiments is included in at least oneembodiment of the invention. The appearances of the phrase “in oneembodiment” in various places in the specification are not necessarilyall referring to the same embodiment.

Some portions of the detailed description are presented in terms ofalgorithms and symbolic representations of operations on data bitswithin a computer memory. These algorithmic descriptions andrepresentations are the means used by those skilled in the dataprocessing arts to most effectively convey the substance of their workto others skilled in the art. An algorithm is here, and generally,conceived to be a self-consistent sequence of steps (instructions)leading to a desired result. The steps are those requiring physicalmanipulations of physical quantities. Usually, though not necessarily,these quantities take the form of electrical, magnetic or opticalsignals capable of being stored, transferred, combined, compared andotherwise manipulated. It is convenient at times, principally forreasons of common usage, to refer to these signals as bits, values,elements, symbols, characters, terms, numbers, or the like. Furthermore,it is also convenient at times, to refer to certain arrangements ofsteps requiring physical manipulations of physical quantities as modulesor code devices, without loss of generality.

It should be borne in mind, however, that all of these and similar termsare to be associated with the appropriate physical quantities and aremerely convenient labels applied to these quantities. Unlessspecifically stated otherwise as apparent from the following discussion,it is appreciated that throughout the description, discussions utilizingterms such as “processing” or “computing” or “calculating” or“determining” or “displaying” or “determining” or the like, refer to theaction and processes of a computer system, or similar electroniccomputing device, that manipulates and transforms data represented asphysical (electronic) quantities within the computer system memories orregisters or other such information storage, transmission or displaydevices.

Certain aspects of the present invention include process steps andinstructions described herein in the form of an algorithm. It should benoted that the process steps and instructions of the present inventioncould be embodied in software, firmware or hardware, and when embodiedin software, could be downloaded to reside on and be operated fromdifferent platforms used by a variety of operating systems.

The present invention also relates to an apparatus for performing theoperations herein. This apparatus may be specially constructed for therequired purposes, or it may comprise a general-purpose computerselectively activated or reconfigured by a computer program stored inthe computer. Such a computer program may be stored in a computerreadable storage medium, such as, but is not limited to, any type ofdisk including floppy disks, optical disks, CD-ROMs, magnetic-opticaldisks, read-only memories (ROMs), random access memories (RAMs), EPROMs,EEPROMs, magnetic or optical cards, application specific integratedcircuits (ASICs), or any type of media suitable for storing electronicinstructions, and each coupled to a computer system bus. Furthermore,the computers referred to in the specification may include a singleprocessor or may be architectures employing multiple processor designsfor increased computing capability.

The algorithms and displays presented herein are not inherently relatedto any particular computer or other apparatus. Various general-purposesystems may also be used with programs in accordance with the teachingsherein, or it may prove convenient to construct more specializedapparatus to perform the required method steps. The required structurefor a variety of these systems will appear from the description below.In addition, the present invention is not described with reference toany particular programming language. It will be appreciated that avariety of programming languages may be used to implement the teachingsof the present invention as described herein, and any references belowto specific languages are provided for disclosure of enablement and bestmode of the present invention.

Finally, it should be noted that the language used in the specificationhas been principally selected for readability and instructionalpurposes, and may not have been selected to delineate or circumscribethe inventive subject matter. Accordingly, the disclosure of the presentinvention is intended to be illustrative, but not limiting, of the scopeof the invention, which is set forth in the following claims.

1. A method of managing host physical memory using a balloon applicationexecuting within a guest virtual machine running on a host platform, themethod comprising: receiving, by the balloon application, allocationparameters from an entity outside a context of the guest virtualmachine, the allocation parameters identifying an amount of memory forthe balloon application to allocate; adjusting an allocated amount ofmemory provided by a guest operating system executing within the guestvirtual machine to the balloon application, the adjusting comprisingincreasing or decreasing the allocated amount of memory according to theallocation parameters; and wherein the balloon application does notwrite to allocated memory except immediately following allocating ofnewly allocated memory when the allocated amount of memory is increased,the balloon application thereby making physical memory backing up theallocated memory available for use by the host platform to assign toanother virtual machine running on the host platform.
 2. The method ofclaim 1, wherein the balloon application receives the allocationparameters from the entity outside the context of the guest virtualmachine by way of a balloon application driver included with a guestoperating system executing in the context of the guest virtual machine.3. The method of claim 1, further comprising transmitting an identity ofthe newly allocated memory that is allocated to the balloon applicationwhen the balloon application increases the allocated amount of memory.4. The method of claim 3, wherein the identity of the newly allocatedmemory is transmitted by causing guest physical page numbers of thenewly allocated memory to be sent to the entity executing outside theguest virtual machine.
 5. The method of claim 4, wherein the balloonapplication sends guest virtual page numbers of the newly allocatedmemory to a balloon application driver, wherein the balloon applicationdriver is caused to convert the guest virtual page numbers of the newlyallocated memory to guest physical page numbers, and then transmit theguest physical page numbers to the entity outside the guest virtualmachine.
 6. The method of claim 1, wherein the entity outside the guestvirtual machine is a resource scheduler executing within the hostplatform.
 7. The method of claim 1, further comprising, when the balloonapplication increases the allocated amount of memory, writing apredetermined value to pages of newly allocated memory immediatelyfollowing the allocating.
 8. The method of claim 7, wherein the balloonapplication does not write to allocated memory after the writing of thepredetermined value so that physical memory backing up the allocatedmemory can be assigned by virtualization software for use by anothervirtual machine running on the host platform.